package com.healthy_software.controller;

import com.healthy_software.dto.*;
import com.healthy_software.entity.User;
import com.healthy_software.result.Result;
import com.healthy_software.service.UserService;
import com.healthy_software.utils.UserIdHolder;
import com.healthy_software.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/auth")
public class UserController {

    @Resource
    private UserService userService;
    /**
     * 登录功能
     * @param loginForm 登录参数，包含手机号、验证码；或者手机号、密码
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginFormDTO loginForm, HttpSession session){
        return userService.login(loginForm,session);
    }

    /**
     * 发送手机验证码
     */
    @PostMapping("/verification-code")
    public Result sendCode(@RequestParam("phone") String phone) {
        // TODO 发送短信验证码并保存验证码
        return userService.sendCode(phone);
    }

    /**
     * 用户注册
     * @param registerDTO
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody RegisterDTO registerDTO){
        return userService.register(registerDTO);
    }

    /**
     * 重置密码
     *
     * @param resetPasswordDTO
     * @return
     */
    @PostMapping("/reset-password")
    public Result resetPassword(@RequestBody ResetPasswordDTO resetPasswordDTO){
        return userService.resetPassword(resetPasswordDTO);
    }

    /**
     * 设置用户基本信息
     */
    @PostMapping("/set-basic-user-info")
    public Result setBasicUserInfo(@RequestBody UserVO userVO){
        return userService.setBasicUserInfo(userVO);
    }

    /**
     * 设置用户健康信息
     */
    @PostMapping("/set-healthy-user-info")
    public Result setUserHealthyInfo(@RequestBody UserHealthyInfoDTO userHealthyInfoDTO){
        return userService.setUserHealthyInfo(userHealthyInfoDTO);
    }

    /**
     * 返回用户信息
     */
    @GetMapping
    public Result<User> getUser(){
        Long userId = UserIdHolder.getCurrentId();
        System.out.println(userId);
        return userService.getUserById(userId);
    }
}
